package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.domain.JsonResult;
import cn.wolfcode.domain.Role;
import cn.wolfcode.qo.EmployeeQueryObject;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IRoleService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;
    @Autowired
    private HttpSession session;

    @RequestMapping("/list")
    @RequiresPermissions(value = {"employee:list","员工列表"},logical = Logical.OR)
    public String list(Model model, @ModelAttribute("qo") EmployeeQueryObject qo,String msg) {
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments", departments);
        PageInfo<Employee> pageInfo = employeeService.query(qo);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("msg", msg);
        return "employee/list";
    }

    @RequestMapping("/delete")
    @RequiresPermissions(value = {"employee:delete","员工删除"},logical = Logical.OR)
    public String delete(Long id) {
        employeeService.delete(id);
        return "redirect:/employee/list.do";
    }


    @RequestMapping("/input")
    @RequiresPermissions(value = {"employee:input","员工编辑"},logical = Logical.OR)
    public String input(Long id, Model model) {
        List<Role> roles = roleService.selectAll();
        model.addAttribute("roles", roles);
        List<Department> departments = departmentService.selectAll();
        model.addAttribute("departments", departments);
        if (id != null) {
            Employee employee = employeeService.selectByPrimaryKey(id);
            model.addAttribute("employee", employee);
        }
        return "employee/input";
    }

    @RequestMapping("/saveOrUpdate")
    @RequiresPermissions(value = {"employee:saveOrUpdate","员工新增或修改"},logical = Logical.OR)
    @ResponseBody
    public JsonResult saveOrUpdate(Employee employee, Long[] ids) {
        if (employee.getId() != null) {
            employeeService.update(employee, ids);
        } else {
            employeeService.insert(employee, ids);
        }
        return new JsonResult();
    }

    @RequestMapping("/update")
    public String updatePassword() {
        return "employee/updatePwd";
    }

    @RequestMapping("/updatePwd")
    @ResponseBody
    public JsonResult updatePwd(String oldPassword, String newPassword) {
        try {
            Employee user = (Employee) session.getAttribute("USER_IN_SESSION");
            Employee employee = employeeService.login(user.getName(), oldPassword);
            if (employee == null) {
                return new JsonResult("修改失败", false);
            }
            employee.setPassword(newPassword);
            employeeService.updatePwd(employee);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult("修改失败", false);
        }
    }

    @RequestMapping("/redi")
    public String redi(Long id,Model model) {
        Employee user = (Employee) session.getAttribute("USER_IN_SESSION");
        return "employee/resetPwd";
    }

    @RequestMapping("/resetPwd")
    @ResponseBody
    public JsonResult resetPwd(String newPassword){
        try {
            Employee user = (Employee) session.getAttribute("USER_IN_SESSION");
            if (user.isAdmin()){
                user.setPassword(newPassword);
                employeeService.updatePwd(user);
                return new JsonResult();
            }
            return new JsonResult("您还不是超级管理员",false);
        } catch(Exception e) {
            e.printStackTrace();
            return new JsonResult("出错了,清联系管理员",false);
        }
    }


    @RequestMapping("/batchDelete")
    @ResponseBody
    public JsonResult batchDelete(Long[] ids){
        employeeService.batchDelete(ids);
        return new JsonResult();
    }

    @RequestMapping("/checkName")
    @ResponseBody
    public HashMap checkName(String name, Long id){
        HashMap hashMap = employeeService.checkName(name, id);
        return hashMap;
    }

    /**
     * 导出功能
     */
    @RequestMapping("/exportXls")
    public void exprotXls(HttpServletResponse response){
        //文件下载头命名
        response.setHeader("Content-Disposition", "attachment;filename=employee.xls");
        //调用业务层方法,创建一个excel文件并返回
        Workbook wb = employeeService.exprotXls();

        //通过response字节流写出去
        try {
            wb.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导入功能
     */
    @RequestMapping("/importXls")
    @ResponseBody
    public JsonResult importXls(MultipartFile file) throws Exception {
        employeeService.importXls(file);
        return new JsonResult();
    }

    /**
     * 同部门查询
     */
    @RequestMapping("/allCheckebox")
    @ResponseBody
    public JsonResult allCheckebox(boolean checked){
        return new JsonResult();
    }


    /**
     * 消息通知跳转
     */
    @RequestMapping("/show")
    public String TZshow(){
        return "notice/show";
    }
}
